using System.Collections;
using UnityEngine;

public class UIFadeFromToAnimation : UIAnimationElement
{
	public float fadeFrom;

	public float fadeTo;

	public float duration;

	public bool useCurve;

	public AnimationCurve curve;

	public Transform transformToChange;

	protected Coroutine myCoroutine;

	public Transform myTransform
	{
		get
		{
			return transformToChange ?? base.transform;
		}
	}

	public UIWidget myWidget
	{
		get
		{
			return myTransform.GetComponent<UIWidget>();
		}
	}

	public UIPanel myPanel
	{
		get
		{
			return myTransform.GetComponent<UIPanel>();
		}
	}

	public override void Play()
	{
		Stop();
		base.Play();
		myCoroutine = StartCoroutine(DoAnimateIn());
	}

	public override void Stop()
	{
		base.Stop();
		if (myCoroutine != null)
		{
			StopCoroutine(myCoroutine);
		}
	}

	private void SetAlpha(float alpha)
	{
		UIWidget uIWidget = myWidget;
		if (uIWidget != null)
		{
			uIWidget.alpha = alpha;
			return;
		}
		UIPanel uIPanel = myPanel;
		if (uIPanel != null)
		{
			uIPanel.alpha = alpha;
		}
	}

	private IEnumerator DoAnimateIn()
	{
		float time = 0f;
		UIWidget widget = myWidget;
		while (time < duration)
		{
			time += RealTime.deltaTime;
			float normTime = time / duration;
			if (useCurve)
			{
				normTime = curve.Evaluate(normTime);
			}
			SetAlpha(Mathf.Lerp(fadeFrom, fadeTo, normTime));
			yield return null;
		}
		SetAlpha(fadeTo);
		OnEnd();
	}
}
